Telegram Group & Telegram Channel
Строковый метод translate позволяет заменять или удалять несколько символов в строке за один раз. Это похоже на множественные вызовы метода replace.

```python
import string

s = "Hello, world!"
print(s.translate(str.maketrans("", "", string.punctuation)))
# Hello world
```

Метод translate ожидает «таблицу перевода», которую мы создаем с помощью вспомогательного метода класса maketrans.

Третий аргумент (string.punctuation в примере) — это строка символов, которые мы хотим удалить.

Значение string.punctuation включает в себя следующие символы:
!#$%&'()*+,-./:;?@[\]^_{|}~`

Суть шпаргалки заключается в том, что она демонстрирует, как эффективно удалить все стандартные знаки препинания из строки в Python, используя комбинацию методов str.maketrans для создания инструкции по удалению и str.translate для применения этой инструкции к строке.

@Python_Community_ru



tg-me.com/Python_Community_ru/2559
Create:
Last Update:

Строковый метод translate позволяет заменять или удалять несколько символов в строке за один раз. Это похоже на множественные вызовы метода replace.

```python
import string

s = "Hello, world!"
print(s.translate(str.maketrans("", "", string.punctuation)))
# Hello world
```

Метод translate ожидает «таблицу перевода», которую мы создаем с помощью вспомогательного метода класса maketrans.

Третий аргумент (string.punctuation в примере) — это строка символов, которые мы хотим удалить.

Значение string.punctuation включает в себя следующие символы:
!#$%&'()*+,-./:;?@[\]^_{|}~`

Суть шпаргалки заключается в том, что она демонстрирует, как эффективно удалить все стандартные знаки препинания из строки в Python, используя комбинацию методов str.maketrans для создания инструкции по удалению и str.translate для применения этой инструкции к строке.

@Python_Community_ru

BY Python Community




Share with your friend now:
tg-me.com/Python_Community_ru/2559

View MORE
Open in Telegram


Python Community Telegram | DID YOU KNOW?

Date: |

Tata Power whose core business is to generate, transmit and distribute electricity has made no money to investors in the last one decade. That is a big blunder considering it is one of the largest power generation companies in the country. One of the reasons is the company's huge debt levels which stood at ₹43,559 crore at the end of March 2021 compared to the company’s market capitalisation of ₹44,447 crore.

The messaging service and social-media platform owes creditors roughly $700 million by the end of April, according to people briefed on the company’s plans and loan documents viewed by The Wall Street Journal. At the same time, Telegram Group Inc. must cover rising equipment and bandwidth expenses because of its rapid growth, despite going years without attempting to generate revenue.

Python Community from no


Telegram Python Community
FROM USA